<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/common/common.jsp" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>"> 
    <title>开处方界面</title>  
    <link rel="stylesheet" href="<%=path %>/plugins/layui/css/layui.css" media="all">
  </head>

  <script src="<%=path %>/plugins/layui/layui.js"></script>
  
  <body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
  <legend>开处方</legend>
</fieldset>
	<div class="layui-row">
	    <div class="layui-col-md12">
	      <div class="layui-row grid-demo">
	        <div class="layui-col-md9 layui-col-md-offset3">
	          <div class="grid-demo grid-demo-bg1">
	            <form class="layui-form layui-form-pane" id="recipeForm" lay-filter="example" action="">
					<div class="layui-form-item">
				    <label class="layui-form-label">学生姓名</label>
				    <div class="layui-input-inline">
				      <input type="text" name="stuName" readonly="readonly" lay-verify="stuName" placeholder="请输入" autocomplete="off" class="layui-input">
				    </div>
				  </div>
				  <div class="layui-form-item">
				    <label class="layui-form-label">年龄</label>
				    <div class="layui-input-inline">
				      <input type="text" name="stuAge" readonly="readonly" lay-verify="stuAge" placeholder="请输入" autocomplete="off" class="layui-input">
				    </div>
				    
				  </div>
				  <div class="layui-form-item">
				    <label class="layui-form-label">性别</label>
				    <div class="layui-input-inline">
				      <input type="text" id="stuSex"  readonly="readonly" name="stuSex" lay-verify="stuSex" placeholder="请输入" autocomplete="off" class="layui-input">
				    </div>
				  </div>
				  <div class="layui-form-item">
				    <label class="layui-form-label">诊疗方法</label>
				    <div class="layui-input-inline">
				      <input type="text" id=""  name="treatMethod" readonly="readonly" lay-verify="" placeholder="请输入" autocomplete="off" class="layui-input">
				    </div>
				  </div>
				  <div class="layui-form-item">
				    <label class="layui-form-label">诊疗结果</label>
				    <div class="layui-input-inline">
				      <input type="text" id=""  name="treatResult" lay-verify="" readonly="readonly" placeholder="请输入" autocomplete="off" class="layui-input">
				    </div>
				  </div>
				  <div class="layui-form-item">
				    <label class="layui-form-label">处方类型</label>
				    <div class="layui-input-inline">
				      <input type="text" name="recipeType" lay-verify="recipeType" readonly="readonly"  placeholder="请输入" autocomplete="off" class="layui-input">
				    </div>
				  </div>
				  <div class="layui-row">
					 <div class="layui-col-md12">
					     <div class="layui-row grid-demo">
					        <div class="layui-col-md5">
					          	<div class="layui-form-item">
								    <label class="layui-form-label">选择药品</label>
								    <div class="layui-input-inline">
										<input type="hidden" name="medicineDesc" id="medList">
							        	<textarea readonly="readonly" id="medName" placeholder="请选择药品" class="layui-textarea"></textarea>
									</div>
									
								</div>
					        </div>
					        
					        <div class="layui-col-md7">
				        		<div class="layui-input-inline">
								  <input type="button" id="select" value="选择" class="layui-btn layui-btn-warm"/> 
								  <input type="hidden" id="medId" name="medId" class="dfinput"/>
							    </div>
					        </div>
					     </div>
					  </div>
				  </div>
					        
				  <div class="layui-form-item" style="margin-left: 150px;margin-top: 80px">
				   <!-- 这行很奇怪啊 为什么会跳到/biz/Recipe_openAdd.action -->
				   <!-- <button class="layui-btn" lay-submit="" lay-filter="submits">保存信息 </button> --> 
				   <input type="button" class="layui-btn" lay-submit="" lay-filter="submit" value="保存信息">
				  </div>
				  	<!-- 瘾君子 -->
				    <input type="hidden" name="stuId">
				    <input type="hidden" name="userId">
				    <input type="hidden" name="userName">
				    <input type="hidden" name="recipeTitle">
				    <input type="hidden" name="treatId">
				</form>  
	          </div>
	        </div>
	        

	      </div>
	    </div>
	 </div>


</body>
  
<script>
layui.use(['form', 'layedit', 'laydate', 'layer', 'jquery'], function(){
  var form = layui.form
  ,layer = layui.layer
  ,layedit = layui.layedit
  ,laydate = layui.laydate
  ,$ = layui.jquery;
  
  //监听提交
  form.on('submit(submit)', function(data){
	$.ajax({
		url: '<%=path%>/biz/Recipe_add.action',
		type: 'POST',
		data: $('#recipeForm').serialize(),
		dataType: 'json',
		success: function(data){
			var json = eval("("+data+")");
			//添加成功
			if(json.isSucceed){
			      layer.open({
			        content: '<div style="padding: 20px 100px;">添加成功</div>'
			        ,btn: '朕知道啦'
			        ,btnAlign: 'c' //按钮居中
			        ,shade: 0 //不显示遮罩
			        ,yes: function(){
			            //返回诊疗列表
						var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引 
						parent.layer.close(index);//再执行关闭当前iframe页面  
						parent.location.reload();//刷新父页面
			        }
			      });
			}else{
				message("添加失败~不解释");
				//关闭当前iframe弹出层
				parent.layer.close();
			}
		},
		error: function(data){
			layer.msg("请求失败");
		}
	});	
    return false;//阻止表单跳转
  });  
  
  //表单初始赋值
  form.val('example', {
    "stuName": '${treat.stuName}'
    ,"stuAge": '${student.stuAge}'
    ,"stuSex": '${student.stuSex}'
    ,"recipeType": '${student.regType}'
    ,"treatMethod": '${treat.treatMethod}'
    ,"treatResult": '${treat.treatResult}'
    ,"stuId": '${treat.stuId}'
    ,"userId": '${treat.userId}'
    ,"userName": '${treat.userName}'
    ,"recipeTitle": '${treat.treatResult}'
    ,"treatId": '${treat.treatId}'
  });
  
  //信息提示弹出层
  function message(data){
      layer.open({
        content: '<div style="padding: 20px 100px;">'+ data +'</div>'
        ,btn: '朕知道啦'
        ,btnAlign: 'c' //按钮居中
        ,shade: 0 //不显示遮罩
        ,yes: function(){
          layer.closeAll();
        }
      });
  };
  
    $('#select').on('click', function(){
 	//打开药品选择页面
	layer.open({
	  type: 2,
	  area: ['85%', '92%'],
	  fixed: false,    //弹框位置不固定
	  maxmin: true,
	  content: '<%=path%>/biz/Medicine_openChoice.action',
	  btn: ['确定保存','取消'],
	  btnAlign: 'c', //按钮剧中
	  anim: 0, //平滑放大
	  yes: function(index, layero){
	    //获取iframe的body元素  
	    var body = layer.getChildFrame('body',index);  
	    //得到iframe页的窗口对象  
	    var iframeWin = window[layero.find('iframe')[0]['name']];                 
	    //执行iframe页的方法，接收药箱  
	    var data = iframeWin.messages();
	    //循环药箱
	    $.each(data, function(key, val) {
		    console.log('id:' + key + ", med:" + val.medName);
		    //获取原先的药品
		    var medName = $("#medName").val();
		    //药品累加(\n为换行符)
		    $("#medName").val(medName+val.medName+" "+val.count+"盒\n");
		    //用于返回给后台(a,b为分隔符)
		    var medList = $("#medList").val()
		    $("#medList").val(medList+val.medId+"a"+val.count+"b");
		    // 如果想退出循环
		    // return false;
		});
		layer.close(index);
	  },
	  success: function(layero, index){
	  	//再打开药箱则清空已选药品(待完善:不清空,根据已选药品在打开的药箱中显示出来)
	  	$("#medName").val("");
	  	$("#medList").val("");
	  },
	  btn2: function(index){
	  	layer.close(index);
	  }
	});
  });
  
  $('#layerDemo .layui-btn').on('click', function(){
    var othis = $(this), method = othis.data('method');
    active[method] ? active[method].call(this, othis) : '';
  });
  
});

</script>
<script>

</script>
</html>
